package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Member;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.adapter.MemberListAdapter;
import com.loongxun.playwithyou.util.ToastView;
import com.loongxun.playwithyou.view.pullableview.MyListener;
import com.loongxun.playwithyou.view.pullableview.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 教练列表
 * Created by Administrator on 2017/9/20.
 */
public class MemberListActivity extends Activity implements View.OnClickListener{

    private Context context;
    private TextView tv_top_title;
    private SendRequest send;

    private List<Member> members = new ArrayList<>();
    private MemberListAdapter mAdapter;
    //预约时间,陪玩时长,
    private String playStartTime = "",hoursNum = "";

    private ListView listView;
    private PullToRefreshLayout ptrl;
    private boolean isFirstIn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_member_list);
        context = MemberListActivity.this;

        playStartTime = getIntent().getStringExtra("playStartTime");
        hoursNum = getIntent().getStringExtra("hoursNum");

        ptrl = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        ptrl.setOnRefreshListener(new MyListener());
        listView = (ListView) findViewById(R.id.content_view);
        initData();
        initView();
    }

    private void initView() {
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("教练列表");
        findViewById(R.id.iv_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.playStartTime = playStartTime;
            send.hoursNum = hoursNum;
            send.findType = "1";
            //预约
            nHttpClient.getAll(ApiInterface.FINDSUIMEMBER, send.tofindSuiMemberJson().toString(), indexHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private MyHttpClientRespenseHandler indexHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            JSONArray itemArray;
            if (succeed == 1) {
                try {
                    itemArray = json.getJSONObject("data").getJSONArray("members");
                    members.clear();
                    for(int i = 0;i < itemArray.length();i++) {
                        JSONObject subItemObject = itemArray.getJSONObject(i);
                        Member member = new Member();
                        member.setMember_id(subItemObject.optInt("member_id", 0));
                        member.setClick_num(subItemObject.optInt("click_num", 0));
                        member.setMember_img(subItemObject.optString("member_img"));
                        member.setMember_name(subItemObject.optString("member_name"));
                        member.setEvaluateInfo(subItemObject.optString("self_evaluation"));
                        members.add(member);
                    }
                    mAdapter = new MemberListAdapter(MemberListActivity.this,members);
                    listView.setAdapter(mAdapter);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                Toast.makeText(context, error_desc, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void faile(String json) {
            Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
        }
    };

    public Handler changeHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Member bean = members.get(msg.what);
            Intent intent = new Intent(context,SaveOrderActivity.class);
            intent.putExtra("playStartTime",playStartTime);
            intent.putExtra("hoursNum",hoursNum);
            intent.putExtra("member",bean);
            startActivity(intent);
        };
    };
}
